$(document).ready(function(){
	$('.butloginaccount').click(function(){
		var url = window.location.href;
		var tmp = url.split("?action=");
		var swap = null;
		if(tmp[1]!=null){
			swap = tmp[1];
		}
		
		if(swap == null)
		{	window.location="./?action=dang-nhap";}
		else{
			window.location="./?action=dang-nhap&returnUrl="+swap;
		}
		return false;
	});
	$('form[name=login]').submit(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var email = $('#Email').val();
		var pass = $('#Password').val();
		if(email != '' && pass != '' && emailReg.test(email)){
			login(email,pass);
		}
		return false;
	});	
	 $(".close").live("click", function (evt){ 
        evt.preventDefault(); 
		$(this).parent().fadeTo(400, 0, function () {
			$(this).slideUp(400);
		});
		return false;
	});
});
function login(email,pass){
	 $.ajax({
	  url:"controller/xl_dang_nhap.php",
	  type:"POST",
	  dataType:"json",
	  data:{Email:email, Password: pass},
	  success: function(data){
		  if(data.success ){
			  if(data.active){
				  $("#loginarea").load('http://localhost/doanhiendai/ #loginarea').fadeIn(1000);
				  var url = window.location.href;
				  var tmp = url.split("returnUrl=");
				  var swap = null;
				  if(tmp[1]!=null){
					  swap = tmp[1];
				  }
				  if(swap == null)
				  {	window.location="./";}
				  else{
					  window.location="./?action="+swap;
				  }
			  }
			  else{
				  displayMessage('#notify','Tài khoản của bạn chưa được kích hoạt','information');
				  $('html, body').animate({scrollTop:0},500);
			  }
		  }
		  else{
			  displayMessage('#notify', 'Email hoặc mật khẩu không chính xác','error');
			  $('html, body').animate({scrollTop:0},500);
		  }
	  }
   });
}
function displayMessage(e, d, f) {
    var g = $(e); 
    g.html("");
    var b = $("<a>").addClass("close").attr("href", "#");
    var c = $("<div>").addClass("notification png_bg " + f).attr("style", "display: none; opacity: 0").append(b).append("<div>" + d + "</div>");
    g.append(c);
    c.show(200, function () {
        c.fadeTo(200, 1)
    })
}
